/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ru.miit.elearning.vtutor.check.object;

import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.JSONException;
import org.json.JSONObject;
import ru.miit.elearning.vtutor.check.CheckObjectResult;
import ru.miit.elearning.vtutor.data.CheckObject;
import ru.miit.elearning.vtutor.ftp.FTPClientDecorator;
import ru.miit.elearning.vtutor.wsclient.VlStudent;

/**
 *
 * @author Cyclone
 */
public abstract class AbstractCheckObject extends CheckObject {

    public AbstractCheckObject(CheckObject co) {
        this.setId(co.getId());
        this.setType(co.getType());
        this.setPath(co.getPath());
        this.setContent(co.getContent());
    }


    /**
     * Check successfull, RESULT_CHECK_OK = 0
     */
    static int RESULT_CHECK_OK = 0;
    /**
     * Check failed, RESULT_CHECK_FAILED = 1
     */
    static int RESULT_CHECK_FAILED = 1;
    /**
     * Check object found, STATUS_FOUND = 0
     */
    static int STATUS_FOUND = 0;
    /**
     * Check object found and content is being checked, STATUS_FOUND_AND_CHECKING = 1
     */
    static int STATUS_FOUND_AND_CHECKING = 1;
    /**
     * Check object not found, STATUS_NOT_FOUND = 2
     */
    static int STATUS_NOT_FOUND = 2;
    /**
     * Exception occured while checking, STATUS_EXCEPTION = 3
     */
    static int STATUS_EXCEPTION = 3;
    /**
     * Combination found, COMB_FOUND = 0
     */
    static int COMB_FOUND = 0;
    /**
     * Combination not found, COMB_NOT_FOUND = 1
     */
    static int COMB_NOT_FOUND = 1;

    public abstract String getTextContent(FTPClientDecorator ftp);

    public abstract CheckObjectResult check(VlStudent student, FTPClientDecorator ftp);

    protected CheckObjectResult getFoundAndCheckedCheckDetails(CheckObjectResult cor, JSONObject jsonCheck) {
        try {
            jsonCheck.put("status", AbstractCheckObject.STATUS_FOUND);
            jsonCheck.put("result", AbstractCheckObject.RESULT_CHECK_OK);
        } catch (JSONException e) {
            Logger.getLogger(AbstractCheckObject.class.getName()).log(Level.SEVERE, null, e);
            e.printStackTrace();
        }

        cor.setChecked(true);
        cor.setDetails(jsonCheck);
        return cor;
    }

    protected CheckObjectResult getNotFoundCheckDetails(CheckObjectResult cor, JSONObject jsonCheck) {
        try {
            jsonCheck.put("status", AbstractCheckObject.STATUS_NOT_FOUND);
            jsonCheck.put("result", AbstractCheckObject.RESULT_CHECK_FAILED);
        } catch (JSONException e) {
            Logger.getLogger(CheckObjectPSType.class.getName()).log(Level.SEVERE, null, e);
            e.printStackTrace();
        }

        cor.setChecked(false);
        cor.setDetails(jsonCheck);
        return cor;
    }
}
